package math;

import java.util.List;

import data.Data;

public class PrecisionRecallF1 {
	int np = 0;
	int tp = 0;
	int fp = 0;
	int n0 = 0;
	int t0 = 0;
	int f0 = 0;
	int nn = 0;
	int tn = 0;
	int fn = 0;
	
	double pp = 0.0;
	double rp = 0.0;
	double p0 = 0.0;
	double r0 = 0.0;
	double pn = 0.0;
	double rn = 0.0;
	
	public PrecisionRecallF1(List<Data> gndTruth, List<Data> result) throws IllegalArgumentException {
		if(gndTruth.size() != result.size()){
			throw new IllegalArgumentException();
		}
		
		for(int i=0; i<gndTruth.size(); i++){
			Data d1 = gndTruth.get(i);
			Data d2 = result.get(i);
			byte sign1 = d1.sign;
			byte sign2 = d2.sign;
			
			if (sign1 > 0){
				np++;
			}
			else if (sign1 < 0){
				nn++;				
			}
			else {
				n0++;
			}
			
			if (sign2 > 0){
				if (sign1 > 0){
					tp++;
				}
				else {
					fp++;
				}
			}
			else if (sign2 < 0){
				if (sign1 < 0){
					tn++;
				}
				else {
					fn++;
				}
			}
			else {
				if (sign1 == 0){
					t0++;
				}
				else {
					f0++;
				}
			}
		}
		pp = 100 * tp / (double) (tp+fp);
		rp = 100 * tp / (double) np;
		p0 = 100 * t0 / (double) (t0+f0);
		r0 = 100 * t0 / (double) n0;
		pn = 100 * tn / (double) (tn+fn);
		rn = 100 * tn / (double) nn;
		
		System.out.println("Num of retrieved true pos/ retrieved pos/ true pos: "+tp+"/"+(tp+fp)+"/"+np);		
		System.out.printf("Precision/Recall/F1 for pos: %.1f/%.1f/%.1f\n", pp, rp, 2*pp*rp/(pp+rp));
		
		System.out.println("Num of retrieved true neu/ retrieved neu/ true neu: "+t0+"/"+(t0+f0)+"/"+n0);		
		System.out.printf("Precision/Recall/F1 for neu: %.1f/%.1f/%.1f\n", p0, r0, 2*p0*r0/(p0+r0));
		
		System.out.println("Num of retrieved true neg/ retrieved neg/ true neg: "+tn+"/"+(tn+fn)+"/"+nn);		
		System.out.printf("Precision/Recall/F1 for neg: %.1f/%.1f/%.1f\n", pn, rn, 2*pn*rn/(pn+rn));
	}

	public int getNp() {
		return np;
	}

	public int getTp() {
		return tp;
	}

	public int getFp() {
		return fp;
	}

	public int getN0() {
		return n0;
	}

	public int getT0() {
		return t0;
	}

	public int getF0() {
		return f0;
	}

	public int getNn() {
		return nn;
	}

	public int getTn() {
		return tn;
	}

	public int getFn() {
		return fn;
	}

	public double getPp() {
		return pp;
	}

	public double getRp() {
		return rp;
	}

	public double getP0() {
		return p0;
	}

	public double getR0() {
		return r0;
	}

	public double getPn() {
		return pn;
	}

	public double getRn() {
		return rn;
	}
}
